package com.zw.yzk.kabu.mapper;

import android.text.TextUtils;

import com.zw.yzk.kabu.data.network.entity.GetShoppingCartEntity;
import com.zw.yzk.kabu.data.network.entity.GoodsDetailsEntity;
import com.zw.yzk.kabu.model.GoodsDetailsModel;
import com.zw.yzk.kabu.model.ShoppingCartGoodsModel;
import com.zw.yzk.kabu.model.ShoppingCartModel;
import com.zw.yzk.kabu.utils.CalculateUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by zhanwei on 2017/3/6.
 */

public class ShoppingCartMapper {

    private GoodsDetailsMapper mapper;

    @Inject
    public ShoppingCartMapper() {
        mapper = new GoodsDetailsMapper();
    }

    public List<ShoppingCartModel> transfer(GetShoppingCartEntity entity) {
        List<ShoppingCartModel> result = new ArrayList<>();
        List<ShoppingCartGoodsModel> goodsList = transfer(entity.result.goodsList);
        List<String> types = new ArrayList<>();
        for (ShoppingCartGoodsModel model : goodsList) {
            String shopId = String.valueOf(model.getShopId());
            if (!types.contains(shopId)) {
                types.add(shopId);
            }
        }
        for (String shopId : types) {
            ShoppingCartModel model = new ShoppingCartModel();
            for (ShoppingCartGoodsModel goods : goodsList) {
                if (shopId.equals(String.valueOf(goods.getShopId()))) {
                    model.setShopName(goods.getShopName());
                    model.setId(goods.getShopId());
                    model.addModels(goods);
                }
            }
            result.add(model);
        }
        return result;
    }

    public ShoppingCartGoodsModel transfer(GetShoppingCartEntity.Goods result) {
        ShoppingCartGoodsModel model = new ShoppingCartGoodsModel();
        model.setId(result.id);
        model.setTypeId(result.typeId);
        model.setShopId(result.shopId);
        model.setShopName(result.shopName);
        model.setSkuId(result.skuId);
        model.setGoodsName(result.goodsName);
        model.setCount(result.count);
        model.setImage(result.image);
        model.setGoodsId(result.goodsId);
        model.setPropertyList(transferProperty(result.propertyList));
        model.setSkuList(transferSku(result.skuList));
        model.setPrice(getSelectedPrice(model));
        model.setSelectedProperty(getSelectedProperty(model));
        model.setFreight(result.freight);

        return model;
    }

    public GoodsDetailsModel.Property transferProperty(GetShoppingCartEntity.Property entity) {
        GoodsDetailsModel.Property model = new GoodsDetailsModel.Property();
        // 属性id
        model.setPropertyId(entity.propertyId);
        // 属性
        model.setContent(entity.content);
        // 子属性列表
        model.setModelList(mapper.transferModel(entity.modelList));

        return model;
    }

    public List<GoodsDetailsModel.Property> transferProperty(List<GetShoppingCartEntity.Property> entityList) {
        List<GoodsDetailsModel.Property> modelList = new ArrayList<>();
        if (entityList == null || entityList.isEmpty()) {
            return modelList;
        }
        for (GetShoppingCartEntity.Property entity : entityList) {
            modelList.add(transferProperty(entity));
        }
        return modelList;
    }

    public GoodsDetailsModel.Sku transferSku(GetShoppingCartEntity.Sku entity) {
        GoodsDetailsModel.Sku model = new GoodsDetailsModel.Sku();
        //库存
        model.setStock(entity.stock);
        // 型号组合id，productid_propertyid:modelid_...组合，需要酱每个属性里面的每一个型号都组合起来
        model.setSkuId(entity.skuId);
        // 优惠价
        model.setOriginalPrice(entity.originalPrice);
        // 原价
        model.setDiscountedPrice(entity.discountedPrice);
        //真实价格
        model.setPrice(CalculateUtils.getRealPrice(entity.originalPrice, entity.discountedPrice));
        return model;
    }

    public List<GoodsDetailsModel.Sku> transferSku(List<GetShoppingCartEntity.Sku> entityList) {
        List<GoodsDetailsModel.Sku> modelList = new ArrayList<>();
        if (entityList == null || entityList.isEmpty()) {
            return modelList;
        }
        for (GetShoppingCartEntity.Sku entity : entityList) {
            modelList.add(transferSku(entity));
        }
        return modelList;
    }

    public List<ShoppingCartGoodsModel> transfer(List<GetShoppingCartEntity.Goods> resultList) {
        List<ShoppingCartGoodsModel> models = new ArrayList<>();
        if (resultList == null || resultList.isEmpty()) {
            return models;
        }
        for (GetShoppingCartEntity.Goods result : resultList) {
            models.add(transfer(result));
        }
        return models;
    }

    private float getSelectedPrice(ShoppingCartGoodsModel model) {
        String skuId = model.getSkuId();
        if (TextUtils.isEmpty(skuId)) {
            return Float.MIN_NORMAL;
        }
        for (GoodsDetailsModel.Sku sku : model.getSkuList()) {
            if (skuId.equals(sku.getSkuId())) {
                return sku.getPrice();
            }
        }
        return Float.MIN_NORMAL;
    }

    private String getSelectedProperty(ShoppingCartGoodsModel model) {
        String skuId = model.getSkuId();
        StringBuilder result = new StringBuilder();
        try {
            String[] tmp = skuId.split("_");
            //第一个item是商品id，跳过
            for (int i = 1; i < tmp.length; i++) {
                for (GoodsDetailsModel.Property property1 : model.getPropertyList()) {
                    for (GoodsDetailsModel.Model model1 : property1.getModelList()) {
                        if (tmp[i].equals(String.valueOf(model1.getPropertyId() + ":" + model1.getId()))) {
                            result.append(model1.getContent()).append(" ");
                        }
                    }
                }
            }
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

}
